/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RSSReader;

import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.rms.*;
  

/**
 *
 * @author Student
 */
public class guiManager  {
    private TextField twitterUserField = null;
    private TextField twitterPass = null;

    protected java.util.Vector rssList;
    protected java.util.Vector rssUrlsList;
    protected java.util.Vector UiElements;
    protected Display displayDevice;
    protected Form refConfigForm;
    protected List showRSSList;
    protected List showRSSUrls;

    private int selectedRSSFeed;
    private int selectedRSSNews;

    private TextField txtNewFeedName;
    private TextField txtNewFeedURL;

    private RecordStore r_store;
    private int userid;

    public String getTwitterUserName()
    {
        return twitterUserField == null ? "" : twitterUserField.getString();
    }

    public String getTwitterPassword()
    {
        return twitterPass == null ? "" : twitterPass.getString();
    }

    public guiManager(Form f, List l, List rssURLS, int userid)
    {
        //intialize Members
        this.UiElements = new Vector();
        this.refConfigForm = f;
        this.showRSSList = l;
        this.showRSSUrls = rssURLS;
        this.userid = userid;
        
        getRSSListFromServer();
    }

    public void addTwitterConfigToForm(Form f)
    {
         String twitterUser = "";
        String twitterPassword = "";
        try
        {


            byte bytesTwitterUser[] = r_store.getRecord(100);
            twitterUser = new String(bytesTwitterUser,0,bytesTwitterUser.length);

            byte bytesTwitterPassword[] = r_store.getRecord(150);
            twitterPassword = new String(bytesTwitterPassword,0,bytesTwitterPassword.length);
        } catch (Exception ex) {}
        f.append(new StringItem("","Bitte geben Sie ihren Benutzernamen für ihr Twitter Profil ein."));
        twitterUserField = new TextField("Benutzername", "", 50, TextField.ANY);
        f.append(twitterUserField);
        f.append(new StringItem("","Bitte geben Sie ihr Passwort für ihr Twitter Profil ein."));
        twitterPass = new TextField("Passwort", twitterPassword, 50, TextField.PASSWORD | TextField.ANY);
        f.append(twitterPass);
    }

    // KONFIGURATIONS BILDSCHIRM
    protected void loadConfigScreen()
    {
        // USER UND PASSWORT AUS RECORDSTORE
        String user = "";
        String password = "";
     
        try
        {
            byte bytesUser[] = r_store.getRecord(0);
            user = new String(bytesUser,0,bytesUser.length);

            byte bytesPassword[] = r_store.getRecord(50);
            password = new String(bytesPassword,0,bytesPassword.length);

          
        } catch (Exception ex) {}

        refConfigForm.append( new StringItem("","Bitte geben Sie ihren Benutzernamen für ihr Online Profil ein."));
        refConfigForm.append(new TextField("Benutzername", user, 50, TextField.ANY));
        refConfigForm.append(new StringItem("","Bitte geben Sie ihr Passwort für ihr Online Profil ein."));
        refConfigForm.append(new TextField("Passwort", password, 50, TextField.PASSWORD | TextField.ANY));

        addTwitterConfigToForm(refConfigForm);
        //refConfigForm.append(new TextField("RSS-FEED URL eingeben",null, 200,TextField.ANY));
    }


    // SUCHE
    protected Form addSearchForm()
    {
        Form frmSearch = new Form("RSS News durchsuchen");
        frmSearch.append(new StringItem("","Bitte geben Sie Ihren Suchbegriff ein"));

        return frmSearch;
    }
    

    // TWITTER POST



    // RSS FEED HINZUFÜGEN
    protected Form addRSSFeedForm()
    {
        Form frmAddRSS = new Form("RSS URL Hinzufügen");
        frmAddRSS.append(new StringItem("","Bitte vergeben Sie eine Bezeichnung"));
        this.txtNewFeedName = new TextField("Bezeichnung", null,50, TextField.ANY);
        frmAddRSS.append(this.txtNewFeedName);
        frmAddRSS.append(new StringItem("","Unter welcher URL ist der Feed erreichbar?"));
        this.txtNewFeedURL = new TextField("URL", null,200, TextField.ANY);
        frmAddRSS.append(this.txtNewFeedURL);
        
        return frmAddRSS;
    }
    protected boolean addRSSFeed(int userid)
    {
        serverConnect feedAdd = new serverConnect();
        feedAdd.setUserID(userid);
        if(feedAdd.addRSSFeed(this.txtNewFeedName.getString(), this.txtNewFeedURL.getString()))
            return true;
        else
            return false;
    }


    // RSS FEED LÖSCHEN
    protected Form delRSSFeedForm()
    {
        Form frmDelRssFeed = new Form("RSS FEED löschen");
        frmDelRssFeed.append(new StringItem("","Wirklich das RSS Feed löschen?"));

        return frmDelRssFeed;
    }
    protected boolean delRSSFeed( int index, int userid )
    {
        String rssID  = ((String[])rssUrlsList.elementAt(index))[1];
        serverConnect feedDel = new serverConnect();
        feedDel.setUserID(userid);
        if(feedDel.delRSSFEed( rssID ))
            return true;
        else
            return false;
    }


    // RSS LISTE VOM SERVER
    protected void getRSSListFromServer()
    {
        serverConnect feeds = new serverConnect();
        feeds.setUserID(userid);
        rssUrlsList = feeds.getRSSList();

        for(int i = 0; i < rssUrlsList.size(); i++)
        {
             this.showRSSUrls.append(((String[])rssUrlsList.elementAt(i))[0], null);
        }
    }


    // RSS FEED ABRUFEN
    protected void loadRSSFeed(int index, int userid, String search)
    {
        String rssID  = ((String[])rssUrlsList.elementAt(index))[1];
        this.selectedRSSFeed = index;
        this.showRSSList.deleteAll();
        this.showRSSList.setTitle("Feed " + ((String[])rssUrlsList.elementAt(index))[0]);

        // FEED VON SERVER KOMPONENTE ZIEHEN
        serverConnect feed = new serverConnect();
        feed.setUserID(userid);

        if(search.length() == 0)
            this.rssList = feed.getRSSFeed(rssID);
        else
            this.rssList = feed.searchRSSFeed(search);

        for(int i = 0; i < this.rssList.size(); i++)
        {
            this.showRSSList.append(((String[])this.rssList.elementAt(i))[1], null);
        }
    }


    // RSS FEED NEWS LESEN
    protected Form readRSSNews(int index)
    {
       this.selectedRSSNews = index;
       String feedText = ((String[])this.rssList.elementAt(index))[2];
       if(feedText.length() < 3)
           feedText = ((String[])this.rssList.elementAt(index))[1];

       // DATUM
       StringItem date = new StringItem("", ((String[])this.rssList.elementAt(index))[0]);
       
       Form frmNews = new Form(((String[])this.rssList.elementAt(index))[1]);
       frmNews.append(date);
       frmNews.append(feedText);

       return frmNews;
    }


    // RSS URL IM HANDY BROWSER ÖFFNEN
    protected String getRSSUrl()
    {
        return ((String[])this.rssList.elementAt(this.selectedRSSNews))[3];
    }
}
